package com.itheima.task;

import com.itheima.common.constant.RedisConstant;
import com.itheima.common.utils.AliOssUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Set;

@Component
public class DeleteImgTask {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private AliOssUtil aliOssUtil;

  @Scheduled(cron = "0 0 1 * * ?")
    public void deleteImg(){
        System.out.println("开始执行定时任务...");
        Set<String> redisSet = redisTemplate.opsForSet().members(RedisConstant.SETMEAL_PIC_RESOURCES);
        Set<String> dbSet = redisTemplate.opsForSet().members(RedisConstant.SETMEAL_PIC_DB_RESOURCES);
        if (redisSet != null && dbSet != null) {
            redisSet.removeAll(dbSet);
            // 遍历差集并删除对应的图片
            for (String imgName : redisSet) {
                aliOssUtil.delete(imgName);
                redisTemplate.opsForSet().remove(RedisConstant.SETMEAL_PIC_RESOURCES, imgName);
                System.out.println("删除图片：" + imgName);
            }

            }
    }
}
